package cn.luoshaocheng.user.service.impl;

import cn.luoshaocheng.basic.constant.RegisterConstant;
import cn.luoshaocheng.basic.exception.BusinessException;
import cn.luoshaocheng.basic.service.impl.BaseServiceImpl;
import cn.luoshaocheng.basic.util.Md5Utils;
import cn.luoshaocheng.basic.util.StrUtils;
import cn.luoshaocheng.user.domain.Logininfo;
import cn.luoshaocheng.user.domain.User;
import cn.luoshaocheng.user.dto.RegisterDto;
import cn.luoshaocheng.user.mapper.LogininfoMapper;
import cn.luoshaocheng.user.mapper.UserMapper;
import cn.luoshaocheng.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
@Service
//只读，事务传播机制=SUPPORTS，有我就用，没有事务就不用
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper logininfoMapper;
//注册操作
    @Override
    public void phoneRegister(RegisterDto dto) {
        String phone = dto.getPhone();
        String Password = dto.getPassword();
        String ComfirmPassword = dto.getComfirmPassword();
        String VerifyCode = dto.getVerifyCode();
        //非空校验
        if(StringUtils.isEmpty(phone)||
                StringUtils.isEmpty(Password)||
                StringUtils.isEmpty(ComfirmPassword)||
                StringUtils.isEmpty(VerifyCode)){
            throw new BusinessException("参数不能为空");
        }//判断两次密码输入是否一致
        if(!Password.equals(ComfirmPassword)){
            throw new BusinessException("两次密码输入不一致，请重新输入！");
        }
        //手机号校验 是否已经注册
        User userPhone = userMapper.findByPhone(phone);
        if(userPhone!=null){
            throw new BusinessException("手机号已注册，请直接登录!");
        }
        //验证码校验是否过期
        Object obj = redisTemplate.opsForValue().get(phone + RegisterConstant.PHONEREGISTER );
        if(obj==null){
            throw new BusinessException("验证码已过期，请重新获取！");
        }
        //校验验证码是否正确
        String redisCode=obj.toString().split(":")[0];
        if(!VerifyCode.equalsIgnoreCase(redisCode)){
            throw new BusinessException("验证码输入错误，请重新输入！");
        }
        //6 保存对象Lonininfo 和 User
        //先去获取Logininfo对象和 User对象
       Logininfo logininfo=dto2Logininfo(dto);
        //通过logininfo保存User对象
        User user=logininfo2User(logininfo);
        //先保存logininfo表，返回自增主键
        logininfoMapper.save(logininfo);
        //吧自增主键赋值给user对象，再保存user
        user.setLogininfo_id(logininfo.getId());
        userMapper.save(user);

    }

    private User logininfo2User(Logininfo logininfo) {
        User user = new User();
        BeanUtils.copyProperties(logininfo,user);

        return user;
    }

    private Logininfo dto2Logininfo(RegisterDto dto) {
        Logininfo logininfo = new Logininfo();
        logininfo.setUsername(dto.getPhone());
        logininfo.setPhone(dto.getPhone());
        logininfo.setType(0);
        //生成盐值
        String salt = StrUtils.getComplexRandomString(32);
        //通过密码和盐值进行 加盐加密
        String dbpassword = Md5Utils.encrypByMd5(salt + dto.getPassword());
        logininfo.setSalt(salt);
        logininfo.setPassword(dbpassword);

        return logininfo;
    }
}

